﻿using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using Heimdallr.Net;

namespace Heimdallr.Sample.ChatRoom.Server
{
    class CustomRequestModule : HttpModule, IHttpRequestModule
    {
        #region Constructor

        public CustomRequestModule()
            : base(WebRequestMethods.Http.Post, "svc-bin/chatroom/request/")
        {
        }

        #endregion

        #region HttpModule Impementation

        public override bool Validate(string username, string password)
        {
            return true;
        }

        public override void Dispose()
        {

        }

        #endregion

        #region IHttpRequestModule Method

        public IEnumerable<Notification> Read(HttpRequestContext request)   // Get notification content from uploaded stream.
        {  
            using (Stream stream = request.GetRequestStream())
            using (MemoryStream data = new MemoryStream((int)request.ContentLength))
            {
                stream.CopyTo(data, 65536);

                return new[] { new BufferedNotification(NotificationLevel.Info, data.ToArray(), "application/json") };
            }
        }

        #endregion
    }
}